package com.lebang.medicalInquiry.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lebang.medicalInquiry.component.OssComponent;
import com.lebang.medicalInquiry.constant.ConfigEnum;
import com.lebang.medicalInquiry.constant.RedisKeyEnum;
import com.lebang.medicalInquiry.exception.BusinessException;
import com.lebang.medicalInquiry.mapper.FigureVoiceMapper;
import com.lebang.medicalInquiry.mapper.PhysicianMapper;
import com.lebang.medicalInquiry.model.po.FigureVoice;
import com.lebang.medicalInquiry.model.po.Physician;
import com.lebang.medicalInquiry.model.vo.PageVo;
import com.lebang.medicalInquiry.utils.HttpClientUtil;
import com.lebang.medicalInquiry.utils.TextUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;

/**
 * @version V1.0
 * @author: zpc
 * @date:
 */
@Service
public class FigureVoiceService {


    @Resource
    private FigureVoiceMapper figureVoiceMapper;

    @Resource
    private PhysicianMapper physicianMapper;

    @Resource
    private OssComponent ossComponent;


    // 人物音频列表
    public PageVo getList(String keyWord, Integer languageId, Integer sex, Integer state, int page, Integer limit) {
        List<FigureVoice> figureVoices = figureVoiceMapper.getList(keyWord, languageId, sex, state, page, limit);
        Integer count = figureVoiceMapper.count(keyWord, languageId, sex, state);
        return new PageVo(figureVoices, count);
    }

    // 操作人物音频
    public void operaFigureVoice(FigureVoice figureVoiceOpearVo) {

        if (TextUtil.isEmpty(figureVoiceOpearVo.getId())) {
            figureVoiceOpearVo.setCreatedTime(LocalDateTime.now());
            figureVoiceOpearVo.setUpdatedTime(LocalDateTime.now());
            figureVoiceOpearVo.setState(1);
            figureVoiceMapper.insertSelective(figureVoiceOpearVo);
        } else {
            figureVoiceMapper.updateByPrimaryKeySelective(figureVoiceOpearVo);
        }
    }

    // 删除 人物id
    public void removeById(Integer figureId) {
        figureVoiceMapper.deleteByPrimaryKey(figureId);

    }


    public String getPushTextRoleVoice(String content, String voiceId) {

//        Physician physician = physicianMapper.selectByPrimaryKey(Integer.parseInt(voiceId));

        FigureVoice figureVoice = figureVoiceMapper.selectByPrimaryKey(Integer.parseInt(voiceId));
        if (figureVoice == null) {
            throw new BusinessException("音频人物未查询到");
        }
        String sub = "mp3";

        String speed = figureVoice.getSpeed() + "%";
        String volume = figureVoice.getVolume() + "%";
        String pitch = "";
//        String dialogueEmoCode = jsonObject.getString("dialogueEmoCode");


        // 获取所有主播情绪
//        List<ZbEmotion> all = zbEmotionMapper.getAll();
//        //  关键词匹配情绪库
//        List<DialogueEmotionVo> allEmoCode = dialogueEmotionMapper.getAllEmoCode();

        // 组合对白情绪id
        InputStream inputStream = null;

        inputStream = HttpClientUtil.generateVoice("eastasia", figureVoice.getVoiceName(), "", volume, speed, figureVoice.getRole(), pitch, null, content);


        String voiceUrl = ossComponent.uploadStream(inputStream, "voice", UUID.randomUUID().toString().replaceAll("-", "") + "." + sub);

        System.out.println(voiceUrl);


        return voiceUrl;
    }


}
